<?php


namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;

class Comment extends Base {


    public function _initialize()
    {
        parent::_initialize();
    }
    public function index(){
        //$map = $this->_search();

        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('user_id')){
                $map['c.user_id'] = input('user_id');
            }
            if(input('username')){
                $map['c.username'] = input('username');
            }
            if(input('goods_name')){
                $map['g.goods_name'] = ['like','%'.input('goods_name').'%'];
            }
            if(input('time_start') && input('time_end')){
                $time_start = strtotime(input('time_start'));
                $time_end = strtotime(input('time_end'));
                $map['c.add_time'] = ['between',[$time_start,$time_end]];
            }
            if($this->sid){
                $map['c.sid'] = $this->sid;
            }
            $count = Db::name('comment')->alias('c')->join('goods g','c.goods_id=g.goods_id')->where($map)->count();

            $field = 'c.*,g.goods_name';
            $list = Db::name('comment')
                ->alias('c')
                ->join('goods g','c.goods_id=g.goods_id')
                ->where($map)
                ->field($field)
                ->order('c.user_id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $img_str = '';
                if($v['img']){
                    $imgs = explode(',',$v['img']);
                    $img_str .= '<div>';
                    foreach($imgs as $img){
                        $img_str .= '<img src="'.$img.'" width="50" height="50"  onclick="showBigImage(this)" />';
                    }
                    $img_str .= '</div>';
                }
                $v['img_str'] = $img_str;
                $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
                $v['show_str'] = $v['is_show']==1?'是':'否';
                $list[$k] = $v;
            }

            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }

        return View::fetch();
    }

    public function detail(){
        $id = I('get.id/d');
        $res = Db::name('comment')->where(array('comment_id'=>$id))->find();
        if(!$res){
            exit($this->error('不存在该评论'));
        }
        if(IS_POST){
            $add['parent_id'] = $id;
            $add['content'] = trim(input('content'));
            $add['goods_id'] = $res['goods_id'];
            $add['add_time'] = time();
            $add['username'] = 'admin';
            $add['is_show'] = 1;
            empty($add['content']) && $this->error('请填写回复内容');
            $row =  Db::name('comment')->insert($add);
            if($row){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
            exit;

        }
        $reply = Db::name('comment')->where(array('parent_id'=>$id))->select(); // 评论回复列表
         
        View::assign('info',$res);
        View::assign('reply',$reply);
        return View::fetch();
    }

    public function excel(){
        //$map = $this->_search();
        $page = input('page');
        $limit = input('limit')?:15;
        $map = [];
        if(input('user_id')){
            $map['c.user_id'] = input('user_id');
        }
        if(input('username')){
            $map['c.username'] = input('username');
        }
        if(input('goods_name')){
            $map['g.goods_name'] = ['like','%'.input('goods_name').'%'];
        }
        if(input('time_start') && input('time_end')){
            $time_start = strtotime(input('time_start'));
            $time_end = strtotime(input('time_end'));
            $map['c.add_time'] = ['between',[$time_start,$time_end]];
        }
        if($this->sid){
            $map['c.sid'] = $this->sid;
        }
        $count = Db::name('comment')->alias('c')->join('goods g','c.goods_id=g.goods_id')->where($map)->count();

        $field = 'c.*,g.goods_name';
        $list = Db::name('comment')
            ->alias('c')
            ->join('goods g','c.goods_id=g.goods_id')
            ->where($map)
            ->field($field)
            ->order('c.user_id desc ')
            ->page($page,$limit)
            ->select();
        foreach($list as $k=>$v){
            $img_str = '';
            if($v['img']){
                $imgs = explode(',',$v['img']);
                $img_str .= '<div>';
                foreach($imgs as $img){
                    $img_str .= '<img src="'.$img.'" width="50" height="50"  onclick="showBigImage(this)" />';
                }
                $img_str .= '</div>';
            }
            $v['img_str'] = $img_str;
            $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
            $v['show_str'] = $v['is_show']==1?'是':'否';
            $list[$k] = $v;
        }
        $title = ['id','会员ID','会员编号','评论内容','评论图片','商品','显示','评论时间'];
        $data = [];
        foreach($list as $v){
            $arr = [
                $v['comment_id'],$v['user_id'],$v['username'],$v['content'],$v['img'],$v['goods_name'],$v['is_show']?'是':'否',$v['add_time']
            ];
            $data[] = $arr;
        }
        return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data,'title'=>$title]);
    }

    /***
     *操作商品评论
     */
    public function del(){
        $type = I('post.type');
        $ids = input('ids/a');
        $row = $row = Db::name('comment')->where('comment_id', 'IN', $ids)->whereOr('parent_id', 'IN', $ids)->delete();
        return json(['status'=>1,'msg'=>'操作成功']);
    }

    public function changeShow(){
        $id = input('id');
        $is_show = Db::name('comment')->where('comment_id',$id)->value('is_show');
        Db::name('comment')->where('comment_id',$id)->update(['is_show'=>$is_show?0:1]);
        return json(['status'=>1,'msg'=>'修改成功']);
    }
}